package com.yugao.fintech.antelope.mps.core.channel.client;

import com.binarywang.spring.starter.wxjava.mp.config.WxMpServiceAutoConfiguration;
import com.binarywang.spring.starter.wxjava.mp.config.WxMpStorageAutoConfiguration;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import com.yugao.fintech.antelope.base.model.module.config.param.WxMpParam;
import com.yugao.fintech.antelope.mps.api.constatnts.ChannelCodeEnum;
import com.yugao.fintech.antelope.mps.assembly.MsgChannelParamAssembly;
import com.yugao.fintech.antelope.mps.core.channel.MsgChannel;
import com.yugao.fintech.framework.core.utils.SpringUtils;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.context.ApplicationContext;

/**
 * 微信公众号客户端
 
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class WxMpMsgClient extends BaseMsgChannelClient {
    private WxMpService wxMpService;
    private WxMpProperties wxMpProperties;
    private WxMpConfigStorage wxMpConfigStorage;

    @Override
    public ChannelCodeEnum channelCode() {
        return ChannelCodeEnum.WX_MP;
    }

    @Override
    public void init() {
        ApplicationContext applicationContext = SpringUtils.getApplicationContext();
        WxMpParam wxMpParam = (WxMpParam) this.getMsgChannel().getParamConfig();
        this.wxMpProperties = MsgChannelParamAssembly.INSTANCES.toWxMpProperties(wxMpParam);

        WxMpStorageAutoConfiguration storageAutoConfiguration = new WxMpStorageAutoConfiguration(applicationContext, this.wxMpProperties);
        this.wxMpConfigStorage = storageAutoConfiguration.wxMpConfigStorage();

        WxMpServiceAutoConfiguration wxMpServiceAutoConfiguration = new WxMpServiceAutoConfiguration();
        this.wxMpService = wxMpServiceAutoConfiguration.wxMpService(this.wxMpConfigStorage, this.wxMpProperties);
    }

    public WxMpMsgClient(MsgChannel msgChannel) {
        super(msgChannel);
    }
}
